﻿#region

using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;

#endregion

namespace FastTrackCabService.AdminPanel
{
    public partial class SupervisorRegistrationForm : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                foreach (var subsidiary in SubsidiaryBLL.GetAllSubsidiaries().OrderBy(sub => sub.CityId))
                {
                    var city = CityBLL.GetCityById(subsidiary.CityId);
                    var item = new ListItem(city.Name, subsidiary.SubsidiaryId.ToString());
                    ddlSubsidiary.Items.Add(item);
                }
            }
        }

        protected void cvEMail_OnServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = !StaffBLL.IsEMailTaken(tbEMail.Text);
        }

        protected void btnRegister_OnClick(object sender, EventArgs e)
        {
            if (IsValid)
            {
                var staffId = StaffBLL.RegisterStaff(tbFName.Text, tbLName.Text, tbEMail.Text, tbTelephone.Text,
                    tbAddress.Text, "Supervisor", Convert.ToByte(ddlSubsidiary.SelectedValue));
                SupervisorBLL.RegisterSupervisor(staffId, ddlType.SelectedValue);
                Response.Redirect("Supervisors.aspx");
            }
        }

        protected void tbEMail_OnTextChanged(object sender, EventArgs e)
        {
            cvEMail.Validate();
        }
    }
}